<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">文章管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 表格 -->
      <el-table :data="articleList" style="width: 100%" border>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="date" label="类型">
          <template slot-scope="scope">
            {{ scope.row.type == 1 ? "文章" : "视频" }}
          </template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者"> </el-table-column>
        <el-table-column prop="date" label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="$router.push(`/home/postpublish/${scope.row.id}`)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeFn(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[1, 4, 8, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getPostAPI } from "@/apis/news.js";
import { getUserId } from "../../utils/localDate";
export default {
  data() {
    return {
      articleList: [],
      pageIndex: 1, //单前页码
      pageSize: 10, //每页条数
      total: 0,
    };
  },
  async created() {
    this.getarticleList();
    // 因为没有token这个值  只能拿到长度
    const { data: res } = await getPostAPI({ pageSize: 1000 });
    // console.log(res);
    this.total = res.data.length;
  },
  methods: {
    //   文章列表
    async getarticleList() {
      const { data: res } = await getPostAPI({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      });
      this.articleList = res.data;
      console.log(this.articleList);
    },
    //   每页条数改变触发
    handleSizeChange(newSize) {
      console.log(newSize);
      this.pageSize = newSize;
      this.getarticleList();
    },
    //   页码发生改变
    handleCurrentChange(nexIndex) {
      console.log(nexIndex);
      this.pageIndex = nexIndex;
      this.getarticleList();
    },
    // 删除按钮
    async removeFn() {
      const removeconfirm = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      console.log(removeconfirm);
      if (removeconfirm == "cancel") return this.$message.info("取消删除");
      this.$message.success("删除成功");
    },
  },
};
</script>

<style lang="less" scoped></style>
